﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_RootCataDAL
    {
        public List<WI_RootCata> GetAllRootCata(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_RootCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.RootCataID);
                ttlCount = query.Count();
                List<WI_RootCata> rootlist = query.Skip(start).Take(limit).ToList();
                return rootlist;
            }
        }

        public WI_RootCata GetRootCata(int rootId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_RootCata rootcata = ctx.WI_RootCata.SingleOrDefault(c => c.RootCataID == rootId);
                return rootcata;
            }
        }

        public void CreateRootCata(string rootCataName, byte? rootCataType, int? sortNum)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_RootCata rootcata = new WI_RootCata
                {
                    RootCataName = rootCataName,
                    RootCataType = rootCataType,
                    RootStatus = (byte)RootCataStatus.Enabled,
                    SortNum = sortNum
                };
                ctx.WI_RootCata.AddObject(rootcata);
                ctx.SaveChanges();
            }
        }

        public bool UpdateRootCata(int rootId, string rootCataName, byte? rootCataType, byte? rootStatus, int? sortNum)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_RootCata rootcata = ctx.WI_RootCata.SingleOrDefault(c => c.RootCataID == rootId);
                if (rootcata != null)
                {
                    rootcata.RootCataName = rootCataName;
                    rootcata.RootCataType = rootCataType;
                    rootcata.RootStatus = rootStatus;
                    rootcata.SortNum = sortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveRootCata(int rootId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_RootCata rootcata = ctx.WI_RootCata.SingleOrDefault(c => c.RootCataID == rootId);
                if (rootcata != null)
                {
                    ctx.WI_RootCata.DeleteObject(rootcata);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
